package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public PageResult page(StudentQueryParam studentQueryParam) {
        PageHelper.startPage(studentQueryParam.getPage(), studentQueryParam.getPageSize());
        Page<Student> pageInfo = studentMapper.list(studentQueryParam);
        return new PageResult<>(pageInfo.getTotal(), pageInfo.getResult());
    }

    @Override
    public void inster(Student student) {
        studentMapper.insert(student);
    }

    @Override
    public Student get(Integer id) {
        Student student = studentMapper.get(id);
        return student;
    }

    @Override
    public void update(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.update(student);
    }

    @Override
    public void deleteByIds(List<Integer> ids) {

        studentMapper.deleteByIds(ids);
    }

    @Override
    public void violation(Integer id, Short score) {
        //扣分不能小于0
        if (score < 0){
            throw new RuntimeException("扣分分数不能小于0");
        }
        studentMapper.violation(id,score);
        studentMapper.update(studentMapper.get(id));
    }


}
